package com.ohos.carbon.slice.componentList;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ComponentViewDataBean;
import com.ohos.carbon.provider.ComponentViewProvider;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ComponentViewSlice extends AbilitySlice {
    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<ComponentViewDataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_component_view);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("ComponentView")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("ComponentView")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("ComponentView", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("ComponentView", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("ComponentView", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        mList = new ArrayList<>();
        mList.add(new ComponentViewDataBean("1", "Class in the Jar", "4:05"));

    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("ComponentView");
//        text = (Text) findComponentById(ResourceTable.Id_text);
//        text.setText(ResourceTable.String_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new ComponentViewProvider(mList, getContext()));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
